package gui;

import java.util.GregorianCalendar;

import msjcalendar.Calendar;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;

import enums.DayEnum;

/**
 * @author sebastian
 *
 */
public class MonthViewComposite extends Composite {

	/**
	 * tables for show events. 
	 */
	private TableViewer[] tables;
	/**
	 * 
	 */
	private GregorianCalendar date;
	/**
	 * 
	 */
	private Calendar calendar;
	
	/**
	 * @return the calendar
	 */
	public Calendar getCalendar() {
		return calendar;
	}

	/**
	 * @param calendar the calendar to set
	 */
	public void setCalendar(final Calendar calendar) {
		this.calendar = calendar;
	}

	/**
	 * 
	 */
	private ISelectionChangedListener selectionListener;
	
	/**
	 * @param parent parent control
	 * @param style SWT-style
	 * @param listener listener for monthview
	 * @param calendar selected calendar
	 */
	public MonthViewComposite(final Composite parent, final int style, final Calendar calendar, 
			final ISelectionChangedListener listener) {
		super(parent, style);
		GridLayout layout = new GridLayout(7, true);
		this.setLayout(layout);
		this.tables = new TableViewer[42];
		this.selectionListener = listener;
		this.calendar = calendar;
		
		for (int i = 0; i < 7; ++i) {
			Label label = new Label(this, SWT.BORDER);
			GridData data = new GridData(SWT.FILL, SWT.FILL, false, false, 
					1, 1);
			label.setLayoutData(data);
			label.setText(DayEnum.values()[i].toString());
		}
		
		this.generateTables();
	}
	
	/**
	 * 
	 */
	public void refresh() {
		for (TableViewer t: this.tables) {
			t.setInput(this.calendar);
			t.refresh();
		}
	}
	
	/**
	 * 
	 */
	public void generateTables() {
		this.tables = new TableViewer[42];
		
		for (int i = 0; i < 42; ++i) {
			this.tables[i] = new TableViewer(this, SWT.BORDER);
			GridData data = new GridData(SWT.FILL, SWT.FILL, true, true, 
					1, 1);
			this.tables[i].getTable().setLayoutData(data);
			this.tables[i].setContentProvider(new EventContentProvider());
			this.tables[i].setLabelProvider(new EventLabelProvider());
			final Table table = this.tables[i].getTable();
			TableViewerColumn column = new TableViewerColumn(this.tables[i], SWT.None);
			column.getColumn().setText("test");
			column.setLabelProvider(new SmallEventLabelProvider());
			table.addControlListener(new ControlListener() {
				@Override
				public void controlResized(final ControlEvent e) {
					table.getColumns()[0].setWidth(table.getSize().x - 5);
				}
				@Override
				public void controlMoved(final ControlEvent e) {
					table.getColumns()[0].setWidth(table.getSize().x - 5);
					
				}
			});
			table.setHeaderVisible(true);
			table.setLinesVisible(true);
			tables[i].addSelectionChangedListener(this.selectionListener);
		}
		
	}

	/**
	 * @param cal calendar
	 */
	public void setDate(final GregorianCalendar cal) {
		if ((this.date == null) || (cal.get(GregorianCalendar.MONTH) 
						!= this.date.get(GregorianCalendar.MONTH))) {
			this.date = (GregorianCalendar) cal.clone();
			cal.add(GregorianCalendar.DAY_OF_MONTH, -(cal.get(GregorianCalendar.DAY_OF_MONTH) - 1));
			int offset = cal.get(GregorianCalendar.DAY_OF_WEEK);
			if (offset == 1) {
				offset = 6;
			} else {
				offset -= 2;
			}
			for (int i = 0; i < 42; ++i) {
				
				if (cal.get(GregorianCalendar.MONTH) == this.date.get(GregorianCalendar.MONTH)) {
					tables[i].getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 
							1, 1));
					if (i < offset) {
						tables[i].getTable().setVisible(false);
						((EventContentProvider) tables[i].getContentProvider()).setDate((GregorianCalendar) cal.clone());
					} else {
						tables[i].getTable().setVisible(true);
						tables[i].getTable().getColumn(0).setText(String.valueOf(cal.get(GregorianCalendar.DAY_OF_MONTH)));
						((EventContentProvider) tables[i].getContentProvider()).setDate((GregorianCalendar) cal.clone());
						cal.add(GregorianCalendar.DAY_OF_MONTH, 1);
					}
				} else {
					tables[i].getTable().setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false, 
							1, 1));
					tables[i].getTable().setVisible(false);
					((EventContentProvider) tables[i].getContentProvider()).setDate(new GregorianCalendar(0, 0, 1));
					cal.add(GregorianCalendar.DAY_OF_MONTH, 1);
				}
			}
		}
	}
}
